use RDF::Redland;

## DEFINE the Database
# DBName - store
# hash-type = bdb
# store on filesystem in db/
my $storage = new RDF::Redland::Storage (
                  "hashes",
                  "store",
                  "new='yes', hash-type='bdb', dir='db/'")
      or die "Failed to create RDF::Redland::Storage\n";

## DEFINE the Model and link it to the store db
my $model = new RDF::Redland::Model ($storage, "")
      or die "Failed to create RDF::Redland::Model for storage\n";
  
## DEFINE the Parser in order to parse the piped input
my $parser = new RDF::Redland::Parser (undef, "application/rdf+xml")
	  or die "Failed to create a parser\n";

## DEFINE turtle Parser in order to parse the turtle schema
my $turtleparser = new RDF::Redland::Parser ("turtle")
	  or die "Failed to create a parser\n";
	  
## DEFINE string var to capture STDIN	  
my $input = "";

## Capture piped STDIN
foreach $line (<>) {
	$input = $input . $line;
}

## I am not sure if this is ok...
my $uri = new RDF::Redland::URI("http://www.tuwien.ac.at/");

## Parse the captured STDIN into the model (triples)
$parser->parse_string_into_model ($input, $uri, $model);

## Uncomment the next block in order to print all added statements (triples)

#my $stream = $model->as_stream;
#while (!$stream->end) {
#  print "Statement: ",$stream->current->as_string,"\n";
#  $stream->next;
#}
#$stream = undef;

## END of block

## Uncomment the next block in order to test the db.
# This block searches for all statements whose subject equals the URI http://tuwien.ac.at/lvs/course/EfSemW-VU-3.0

#my $testtemplate = new RDF::Redland::Statement(
#					new RDF::Redland::URI("http://tuwien.ac.at/lvs/course#EfSemW-VU-3.0"), 
#					undef,
#					undef);
					
#$stream = $model->find_statements ($testtemplate);
#while (!$stream->end) {
#   my $statement = $stream->current;
#   print "Matching Statement: ", $statement->as_string,"\n";
#   $stream->next;
#}
#$stream   = undef;
#$testtemplate = undef;
## END of block



undef $/;
my $rdfs = <DATA>;

## Parse the captured STDIN into the model (triples)
$turtleparser->parse_string_into_model ($rdfs, $uri, $model);

#print $rdfs; #uncomment this to print the ttl schema.

## UNDEFINE the model and the db for clean and in older versions of perl
$model = undef;
$storage = undef;

__DATA__
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix lvs: <http://tuwien.ac.at/lvs/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix owl:     <http://www.w3.org/2002/07/owl#> .
@prefix default: <http://www.manuel-petrov.com/semweb_lab01#> .
@prefix c_type: <http://www.tuwien.ac.at/coursetype/> .
    
    
#############################################################
# Study
#############################################################
lvs:Study a rdfs:Class ;
	rdfs:comment "This tag describes a single study element" .

lvs:StudyInternalId a owl:objectProperty ;
	rdfs:domain lvs:Study ;
	rdfs:range xsd:string .

#############################################################
# ExamCourse 
#############################################################
lvs:ExamCourse a rdfs:Class;
	rdfs:comment "This tag describes a particular exam course element" .

#############################################################
# Description (coursetype) 
#############################################################
lvs:CourseType a rdfs:Class ;
	rdfs:comment "This is a type of a Course (to be referenced)" .
	

#############################################################
# CourseCategory
#############################################################
lvs:CourseCategory a rdfs:Class;
	rdfs:comment "This tag describes a particular course category" .

lvs:CourseCategoryInterlanID a owl:objectProperty ;
	rdfs:domain lvs:CourseCategory ;
	rdfs:range xsd:string .


#############################################################
# Course
#############################################################
lvs:Course a rdfs:Class;
	rdfs:comment "This tag represents a Course in the university" .

lvs:CourseHasType a owl:objectProperty ;
	rdfs:domain lvs:Course ;
	rdfs:range lvs:CourseType .
	
lvs:ECTSCount a owl:objectProperty ;
	rdfs:domain lvs:Course ;
	rdfs:range xsd:double .

lvs:SemesterHours a owl:objectProperty ;
	rdfs:domain lvs:Course ;
	rdfs:range xsd:string .

lvs:CourseOccurrences a owl:objectProperty ;
	rdfs:domain lvs:Course .

lvs:CourseOccurrence a owl:objectProperty ;
	rdfs:domain lvs:CourseOccurrences .

lvs:CourseOccurrenceHasStudy a owl:objectProperty ;
	rdfs:domain lvs:CourseOccurrence ;
	rdfs:range lvs:Study .

lvs:CourseOccurrenceHasCategory a owl:objectProperty ;
	rdfs:domain lvs:CourseOccurrence ;
	rdfs:range lvs:CourseCategory .

lvs:ProposedSemester a owl:objectProperty ;
	rdfs:domain lvs:CourseOccurrence .

lvs:CourseOccurrenceHasExamCourse a owl:objectProperty ;
	rdfs:domain lvs:CourseOccurrence .

lvs:CourseMandatoryInCourseOccurrence a owl:objectProperty ;
	rdfs:domain lvs:CourseOccurrence ;
	rdfs:range xsd:integer .

lvs:Divisions a owl:objectProperty ;
	rdfs:domain lvs:Course .

lvs:Division a owl:objectProperty ; 
	rdfs:domain lvs:Divisions .

#############################################################
# CourseInstance
#############################################################

lvs:CourseInstance a rdfs:Class ;
	rdfs:comment "This is a instance of a course at the university" .

lvs:InstanceOfCourse a owl:objectProperty ;
	rdfs:domain lvs:CourseInstance .

lvs:CourseInstanceYear a owl:objectProperty ;
	rdfs:domain lvs:CourseInstance ;
	rdfs:range xsd:integer .

lvs:CourseInstanceSemester a owl:objectProperty ;
	rdfs:domain lvs:CourseInstance ;
	rdfs:range xsd:string .
	
lvs:CourseInstanceBegin a owl:objectProperty ;
	rdfs:domain lvs:CourseInstance ;
	rdfs:range xsd:date .

lvs:CourseInstanceEnde a owl:objectProperty ;
	rdfs:domain lvs:CourseInstance ;
	rdfs:domain xsd:date .

lvs:CourseInstanceLecturer a owl:objectProperty ;
	rdfs:domain lvs:CourseInstance ;
	rdfs:range xsd:string .